function [result, Ir ] = character_Segmentation2( img, IS_DEBUG, FOLDER_RESULT,file_name )
%CHARACTER_SEGMENTATION Summary of this function goes here
%   Detailed explanation goes here

z = size(img,3);
if z==3
    img = rgb2gray(img);
end
[h,w] = size(img);
if(w/h>1.5)
maxwidth = 256;
else
   maxwidth = 196; 
end
img=imresize(img,[NaN maxwidth]);

xmin= 5;
ymin= 0;
[maxhigh, maxwidth] = size(img);
width = maxwidth - 2*xmin;
high = maxhigh-2*ymin;
img = imcrop(img, [xmin ymin width high]);



im1=medfilt2(img,[3 3]); %Median filtering the image to remove noise%
BW = edge(im1,'canny'); %finding edges
BW = bwmorph(BW,'bridge');
msk=[0 0 0 0 ;
    0 1 1 0 ;
    0 1 1 0 ;
    0 0 0 0 ];
B=conv2(double(BW),double(msk),'same');
im2 = imfill(B,'hole');

SE = ones(1,5);
im2 = imopen(im2,SE);
SE = ones(5,1);
im2 = imopen(im2,SE);
SE = ones(1,5);
im2 = imopen(im2,SE);
im2 = bwareaopen(im2, 200);
Ir = im2;
Ib = Sauvola(img,[0.1 128 15]);
Ib = bwareaopen(~Ib, 100);
cost = 10;
im2_ivn = im2';
[~, vector] = breakCost(im2_ivn, cost);
[ arrLineC, arrLineS] = getLine(vector>cost, Ib', im2_ivn);
result = {};
numResult = 0;

for iL = 1:length(arrLineC)
    aSegment = arrLineC{iL};
    h = size(aSegment,1);
    cc =  bwconncomp(aSegment, 8);
    featureData = regionprops(cc,'BoundingBox');
    
    for i=1:cc.NumObjects
        
        width = featureData(i).BoundingBox(3);
        height = featureData(i).BoundingBox(4);
        
        if(width>5 && width<60 && height>h*0.5)
            subimg1 = imcrop(aSegment,featureData(i).BoundingBox);
            subimg2 = imcrop(arrLineS{iL},featureData(i).BoundingBox);
            ftemp = imfill(subimg2,'hole');
            ftemp = xor(ftemp,subimg2);
            
            subimg = xor(subimg1, ftemp);
            subimg = bwareaopen(subimg, 150);
            
            CC1 = bwconncomp(subimg,8);
            %         fd = regionprops(CC1,'BoundingBox');
            numPixels = cellfun(@numel,CC1.PixelIdxList);
            [~,idx] = max(numPixels);
            grain = false(size(subimg));
            grain(CC1.PixelIdxList{idx}) = true;
            subimg = grain;
            v1 = sum(subimg,1);
            v2 = (sum(subimg,2))';
            x1 = find(v1>0,1,'first');
            x2 = find(v1>0,1,'last');
            y1 = find(v2>0,1,'first');
            y2 = find(v2>0,1,'last');
            % x y w h
            if(max(v1) <1 || max(v2)<1)
                subimg = subimg2;
            else
                subimg = imcrop(subimg,[x1 y1 (x2-x1-1) (y2-y1-1)]);
            end
            %         subimg = imcrop(img,featureData(i).BoundingBox);
            %         H = fspecial('gaussian' ,[5 5],2); % create unsharp mask
            %         subimg = imfilter(subimg,H);
            %         subimg = Sauvola(img,[0.1 128 15]);
            numResult = numResult +1;
            result{numResult}= subimg;
        end
        
    end
end

% 
% highO = zeros(1,length(resulttemp));
% for i=1:length(resulttemp)
%     highO(1,i) = size(resulttemp{i},1);
% end
% meanValue = mean(highO);
% num = 0;
% for i=1:length(resulttemp)
%     if (highO(1,i) > (meanValue -10)) && (highO(1,i) < (meanValue +10))
%         temp1 = resulttemp{i};
%          H = fspecial('gaussian' ,[5 5],2); % create unsharp mask
%         temp1 = imfilter(temp1,H);
%         w = size(temp1,2);
%         if(w<16)
%             temp1= imresize(temp1, [64 NaN]);
%             p1=floor((64-w)/2);
%             p2=64-w-p1;
%             temp1 = padarray(temp1, [0 p1], 0, 'pre');
%             if p2>0
%                 temp1 = padarray(temp1, [0 p2], 0, 'post');
%             end
%         end
%         tempim = imresize(temp1,[64 64]);
%         tempim = bwareaopen(tempim, 150);
%         msk=[0 0 0 0 ;
%             0 1 1 0 ;
%             0 1 1 0 ;
%             0 0 0 0 ];
%         tempim=conv2(double(tempim),double(msk));
%         num = num +1;
%         result{num} = logical(tempim);
%         
%     end
% end

if IS_DEBUG
    numCharacter = length(result)
    col = ceil(sqrt(numCharacter));
    row = ceil(numCharacter/col);
    figure(2);
    for i=1:numCharacter
        subplot(row, col, i);
        imshow(result{i});
    end
    
end

end

